<?php echo $this->render('/public/header'); ?>
<?php use yii\helpers\Url; ?>
    <div id="main" class="col-xs-12">
        <div class="form-inline">
            <a href="/statics/template/receipt.xlsx" class="layui-btn layui-btn-normal"
               style="margin-right:20px">收款表格模板</a>
            <div class="layui-box layui-upload-button">
                <form target="layui-upload-iframe" method="post" key="set-mine" enctype="multipart/form-data" action="">
                    <input type="file" name="file1" lay-title="批量上传" lay-type="file" class="layui-upload-file"></form>
                <span class="layui-upload-icon"><i class="layui-icon"></i>批量上传</span></div>
            <input type="text" class="form-control" style="margin-left:10px" onkeypress="get_number(this)" id="cname"/>
            <button class="layui-btn layui-btn-normal" style="margin-left:10px" onclick="get_number_by_name()">搜索
            </button>
            该用户编号是:<span id="cnumber"></span>
        </div>
        <br/>
        <br/>
        <div id="tablelist" style="height:400px">
        </div>
        <div>
            <button class="btn btn-primary" onclick="get_table_data()">提交</button>
        </div>
    </div>
    <script data-jsfiddle="table">
        var data = [
            {
                'date': '', 'name': '', 'country': '', 'number': '', 'customer_name': '', 'service_name': '',
                'pay_type': '', 'money_type': '', 'invocie_num': '', 'in_come': '', 'total_fee': '', 'fee': '',
                'email': '', 'remark': '', 'pay_from': ''
            },
            {
                'date': '', 'name': '', 'country': '', 'number': '', 'customer_name': '', 'service_name': '',
                'pay_type': '', 'money_type': '', 'invocie_num': '', 'in_come': '', 'total_fee': '', 'fee': '',
                'email': '', 'remark': '', 'pay_from': ''
            },
            {
                'date': '', 'name': '', 'country': '', 'number': '', 'customer_name': '', 'service_name': '',
                'pay_type': '', 'money_type': '', 'invocie_num': '', 'in_come': '', 'total_fee': '', 'fee': '',
                'email': '', 'remark': '', 'pay_from': ''
            },
            {
                'date': '', 'name': '', 'country': '', 'number': '', 'customer_name': '', 'service_name': '',
                'pay_type': '', 'money_type': '', 'invocie_num': '', 'in_come': '', 'total_fee': '', 'fee': '',
                'email': '', 'remark': '', 'pay_from': ''
            }
        ];
        var container = document.getElementById('tablelist');

        hot = new Handsontable(container, {
            data: data,
            height: "400px",
            minSpareRows: 1,
            colHeaders: [
                '日期', '交易对手方名称', '国家', '编号', '客户', '业务员', '类型',
                '币种', 'invocie编号', '收入', '支出', '费用', '发件人邮箱地址/交易号/监控号', '备注', '支付来源'
            ],
            dropdownMenu: true,
            contextMenu: true,
            stretchH: 'all',
            columns: [
                {
                    data: 'date',
                    type: 'date',
                    dateFormat: 'YYYY-MM-DD',
                    correctFormat: true,
                    defaultDate: (new Date().getYear())
                },
                {
                    data: "name",
                },
                {
                    data: "country"
                },
                {
                    data: "number"
                },
                {
                    data: "customer_name"
                },
                {
                    data: "service_name",
                    editor: 'select',
                    selectOptions: <?php echo $this->params['saleman'];?>
                },
                {
                    data: "pay_type",
                    editor: 'select',
                    selectOptions: {1: '支出', 2: "退款", 3: '收款'}
                },
                {
                    data: "money_type",
                    editor: 'select',
                    selectOptions: ["USD", "RMB", "EUR"]
                },
                {
                    data: "invocie_num"
                },
                {
                    data: "in_come",
                    type: 'numeric',
                    format: "0.00"
                }
                ,
                {
                    data: 'total_fee',
                    type: 'numeric',
                    format: "0.00"
                },
                {
                    data: 'fee',
                    type: 'numeric',
                    format: "0.00"
                },
                {
                    data: "email"
                },
                {
                    data: "remark"
                },
                {
                    data: "pay_from",
                    editor: 'select',
                    selectOptions: {"1": 'PayPal', "2": 'T/T', "3": '西联',"4":'邮政'}
                }
            ]

        });

    </script>
    <script>
        function get_number_by_name() {
            var name = $("#cname").val();
            var url = "<?php echo Url::to(['api/search_customer']);?>";
            $.post(url, {name: name}, function (res) {
                if (res.code == 1) {
                    $("#cnumber").html(res.data.number + "--" + res.data.customer_name);
                }
                else {
                    $("#cnumber").html("");
                }
            }, 'json');
        }

        function get_number(e) {
            var url = "<?php echo Url::to(['api/search_customer']);?>";
            $.post(url, {name: e.value}, function (res) {
                if (res.code == 1) {
                    $("#cnumber").html(res.data.number + "--" + res.data.customer_name);
                }
                else {
                    $("#cnumber").html("");
                }
            }, 'json');
        }

        function get_table_data() {
            if (layer.confirm('确认提交', {}, function () {
                    if (data[0][0] == "") {
                        layer.alert("未提交数据");
                        return false;
                    }
                    layer.load(0, {shade: false});
                    var url = "<?php echo Url::to(['finance/insert_income']);?>";
                    $.post(url, {data: data}, function (res) {
                        layer.close('loading');
                        if (res.code == 1) {
                            layer.confirm(res.msg, {}, function () {
                                window.location.reload();
                            })
                        } else if (res.code == 0) {
                            layer.alert(res.msg)
                        }
                    }, 'json')
                })) ;
        }

        layui.upload({
            url: '<?php echo Url::to(['finance/receipt_upload_excel']);?>'
            , method: 'post'
            , success: function (res) {
                if (res.code == 1) {
                    layer.confirm('上传成功', {}, function () {
                        window.location.reload();
                    })
                }
                else {
                    layer.alert(res.msg);
                }
            }
        });
    </script>
<?php echo $this->render('/public/footer'); ?>